<?php

class OOWP_FormatSlot extends OOWP_PyList
{

    public function __construct(OOWP_Formattable $client, array $classes = array( ))
    {
        $this->dict['client'] = $client;
        parent::__construct($classes);
    } // __construct

    public function __toString( )
    {
        return get_class( ) . '([' . trim(implode(', ', $this->container)) . '])';
    } // __toString

    protected function getClient( )
    {
        return $this->dict['client'];
    }

    private function attach($obj)
    {
        if ($obj instanceof OOWP_Formattable) {
            $obj->parent = $this->client;
        }
    } // attach

    private function detach($obj)
    {
        if ($obj instanceof OOWP_Formattable) {
            $obj->parent = null;
        }
    } // detach

    public function append($item)
    {
        parent::append($item);
        $this->attach($item);
    } // append

    public function extend($items)
    {
        parent::extend($items);
        foreach ($items as $item) {
            $this->attach($item);
        }
    } // extend

    public function insert($index, $item)
    {
        parent::insert($index, $item);
        $this->attach($item);
    } // insert

    public function pop($index)
    {
        $popped = parent::pop($index);
        $this->detach($popped);
        return $popped;
    } // pop

    /* ARRAY ACCESS */

    public function offsetSet($index, $item)
    {
        $old_item = null;
        if ($this->offsetExists($index)) {
            $old_item = $this[$index];
        }
        parent::offsetSet($index, $item);
        $this->attach($item);
        if (!is_null($old_item)) {
            $this->detch($old_item);
        }
    } // offsetSet

    public function offsetUnset($offset)
    {
        if ($this->offsetExists($offset)) {
            $this->detach($this[$offset]);
        }
        parent::offsetUnset($offset);
    } // offsetUnset

}
